
package com.xinzhidi.web.city.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinzhidi.common.core.redis.RedisCache;
import com.xinzhidi.common.utils.TreeUtil;
import com.xinzhidi.web.city.domain.AllAddress;
import com.xinzhidi.web.city.domain.City;
import com.xinzhidi.web.city.domain.vo.CityVo;
import com.xinzhidi.web.city.mapper.CityMapper;
import com.xinzhidi.web.city.service.CityService;
import com.xinzhidi.web.plot.domain.entity.CPlot;
import com.xinzhidi.web.plot.service.impl.CPlotServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @ClassName CityServiceImpl
 * @Description: 实现类
 * @Author cx
 * @Date 2023-05-23 00:03
 * @Version V1.0
 **/
@Service
@RequiredArgsConstructor
public class CityServiceImpl extends ServiceImpl<CityMapper, City> implements CityService {

    private final CityMapper cityMapper;

    private final CPlotServiceImpl plotService;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private CityService cityService;

    @Override
    public List<City> getCityList(Integer pid) {
        QueryWrapper<City> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("Pid", pid);
        queryWrapper.select().orderByAsc("Id");
        return cityMapper.selectList(queryWrapper);
    }

    @Override
    public List<CPlot> getPlotListByRegion(String area) {
        LambdaQueryWrapper<CPlot> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CPlot::getArea, area);
        List<CPlot> list = plotService.list(wrapper);
        return list;
    }

    /**
     * 功能描述：获取所有省市区地址
     *
     * @Return:com.dkm.modules.wx.city.model.AllAddress 结果地址
     * @Author: Guo Liangbo
     * @Date: 2019/11/7 23:08
     */
    @Override
    public AllAddress getAllAddress() {
        List<City> allCity = cityMapper.selectList(new QueryWrapper<>());
        AllAddress allAddress = new AllAddress();
        // 获取省份
        List<City> provinceList = new ArrayList<>();
        HashMap<String, String> provinceMap = new HashMap<>();
        // 获取城市
        List<City> cityList = new ArrayList<>();
        HashMap<String, String> cityMap = new HashMap<>();
        // 获取乡镇
        List<City> countryList = new ArrayList<>();
        HashMap<String, String> countryMap = new HashMap<>();

        //获取省份
        for (City city : allCity) {
            if (0 == city.getPid()) {
                provinceList.add(city);
                provinceMap.put(city.getId(), city.getName());
            }
        }
        allAddress.setProvince_list(provinceMap);
        for (City city : allCity) {
            for (City province : provinceList) {
                if (city.getPid().equals(province.getId())) {
                    cityList.add(city);
                    cityMap.put(city.getId(), city.getName());
                }
            }
        }
        allAddress.setCity_list(cityMap);
        for (City city : allCity) {
            for (City cityParent : cityList) {
                if (city.getPid().equals(cityParent.getId())) {
                    countryList.add(city);
                    countryMap.put(city.getId(), city.getName());
                }
            }
        }
        allAddress.setCounty_list(countryMap);
        return allAddress;
    }

    @Override
    public List<CityVo> getCityTreeList() {
        List<CityVo> voList = new ArrayList<>();
        List<City> list = cityMapper.selectList(new QueryWrapper<>());
        List<CityVo> plotList = plotService.getPlotCityList();
        if (!CollectionUtils.isEmpty(list)) {
            CityVo vo;
            for (City city : list) {
                vo = new CityVo();
                vo.setId(city.getId());
                vo.setNodeId(String.valueOf(city.getId()));
                vo.setParentNodeId(String.valueOf(city.getPid()));
                vo.setNodeName(city.getName());
                voList.add(vo);
            }
        }
        voList.addAll(plotList);
        return TreeUtil.buildTree(voList);
    }

    @Override
    public Map<String, City> getAreaMap() {
        Map<String, City> idMapCity = redisCache.getCacheObject("idMapCity");
        if (idMapCity == null) {
            idMapCity = cityMapper.selectList(new QueryWrapper<>()).stream().collect(Collectors.toMap(City::getId, Function.identity(), (key1, key2) -> key1));
            redisCache.setCacheObject("idMapCity", idMapCity, 3600, TimeUnit.SECONDS);
        }
        return idMapCity;
    }

}
